<?php
namespace App\Http\Controllers\Wx;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ProductRepo;
use App\Repos\ShopRepo;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {       
        $rs['status'] = true;          
        $where['cate_id'] = trim($request->cate_id);
        if($request->city) {
            $where['city'] = trim($request->city);
        }
        $list = ProductRepo::pages($where);
        $rs['data'] = $list;        
        return response()->json($rs);
    }


    // 根据tag 搜索
    public function search(Request $request)
    {
        $rs['status'] = true;   
        
        $where['keyword'] = trim($request->keyword);
        if($request->orderby) {
            $order['orderby']  = $request->orderby;
            $order['orderway'] = $request->orderway;
        }

        if($request->city) {
            $where['city'] = $request->city;
        }
        $list = ProductRepo::getListByTag($where,$order);
        $rs['data'] = $list;
        
        return response()->json($rs);

    }



    public function info(Request $request)
    {
        $rs['status']     = true;
        $where['status']  = 1;        
        if($request->id) {
            $where['id'] = (int)$request->id;
        }      

        $data          = ProductRepo::find($where);
        if(!$data) {
            $rs['status'] = false;
            $rs['msg'] = '该商品已下架，看看其它商品吧';
            return response()->json($rs);
        }
        
        $data['imgs']  = ProductRepo::imgs($data->id);
        $data['specs'] = ProductRepo::getSpecs($data->id);

        // 商品服务
        $data['service'] = ProductRepo::getService($data->service_id);

        // 进行中的拼团信息
        $data['groupList'] = ProductRepo::groupList($data);

        // 以团人数
        $data['sale_num'] = ProductRepo::grouped($data->id);

        //商户信息
        $shopWhere['id']  = $data->shop_id;
        $shopFileds       = ['id','name','address','phone'];
        $data['shopInfo'] = ShopRepo::find($shopWhere,$shopFileds);

        $rs['data'] = $data;
        
        return response()->json($rs);
    }

   

   

   

    
}
